
var commentCnt = 0;
//截取链接id数据
function GetQueryString(name)
	{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
	}
//获取最近3条
function getRecentPost(data){
	
	for (var i = 0; i <data.length; i++) {
		var	recent=$("<li><a href='single.html?id="+data[i][0]+"'>"+data[i][1]+"</a></li>");
		recent.appendTo($("#recentpost"));
	}
}

function doSubmitForm() {
    var form = document.getElementById('ipt-content');
   // showSearchResult(form.value);
    window.location.href="category.html?kind="+form.value;
}


$(function () {

	var id=GetQueryString("id");

	if(id !=null && id.toString().length>0)
	{	
		var reMethod = "GET";
		$.ajax({
			type: reMethod,
			url: "blog_searchBlogArticleById.action",
			data: "id="+id,
			dataType: 'json',
			success: function(datas) {
				if(datas.status != 200){
					return;
				}
				var result = datas.data.blog;
				$('#blogid').val(result.id);
				$('#author').html(result.author);
				$('#title').html(result.title);
				$('#kind').html(result.kind);
				$('#pubdate').html(result.pubdate);
				$('#form-content').html(result.mainText);
				$("#comment_count").html(result.messageCount+"次评论");
				commentCnt = result.messageCount;
				$("#blogTitile").val(result.title); 
				rerderComment(datas.data.comment);
				
			}
		});
		function rerderComment(data) {
			$("#comment_list").children('li').remove();
			var i=0;
			var user,
				time,
				body,
				comment;
			if(typeof(data) != undefined && data.length>0){
				for(i = 0;i < data.length;i++){
					comment = $("<li><dl></dl></li>");
					body=$("<dt><font style='font-size:15px'>"+data[i].msgContext+"</font></dt>");
					body.appendTo(comment);
					user=$("<dd style='text-align:right;'><font style='font-size:12px;'>by "+data[i].visitorName+"</font></dd>");
					user.appendTo(comment);
					time=$("<dd style='text-align:right;'><font style='font-size:12px;'>"+data[i].formatTime+"</font></dd>");
					time.appendTo(comment);
					comment.appendTo($("#comment_list"));
				}
			}else{
				comment = $("<li><dl class='muted'><em>暂无评论 ≡(▔﹏▔)≡</em></dl></li>");
				comment.appendTo($("#comment_list"));
			}
		}
		reMethod = "POST";
		$.ajax({
			type: reMethod,
			url: "blog_requestSomeBlogByAttr.action",
			data: {startId:0,limit:3,attr:"title"},
			dataType: 'json',
			success: function(result) {
				if(result.status != 200){
					return;
				}
				var data = result.data;
				getRecentPost(data);
			}
		});
	}
	 
	 var usernameObj = document.getElementById("username");
	 var contentObj = document.getElementById("commentContent");
	 var emailObj = document.getElementById("email");
	 commentValidate();
	 
	 function trim(s){
		   return s.replace(/^\s+|\s+$/g,"");
	 }  
	 
	 function checkUserName(){
			var span = document.getElementById("usernameSpan");
			if(span.style.display != "block"){
				return;
			}
			var usernameValue = trim(usernameObj.value);
			var usernameRegex = /^[a-zA-Z_]\w{0,9}$/;
			var msg ="&nbsp;&nbsp;&nbsp;<img src='img/true.png' style='width:12px'>";
			if(usernameValue == null || usernameValue == "")
			    msg ="<font color='red'>求英雄留下昵称!</font>";
			   span.innerHTML = msg;
			return msg == "&nbsp;&nbsp;&nbsp;<img src='img/true.png' style='width:12px'>"; 
		};

		function checkEmail() {
			var span = document.getElementById("emailSpan");
			if(span.style.display != "block"){
				return;
			}
			 var emailObjValue = emailObj.value;
			   var emailRegex = /^[\w-]+@([\w-]+\.)+[a-zA-Z]{2,3}$/;
			   var msg ="&nbsp;&nbsp;&nbsp;<img src='img/true.png' style='width:12px'>";
			   if(!emailObjValue)
			      msg = "<font color='red'>英雄，留下Email啊!</font>";
			   else if(!emailRegex.test(emailObjValue))
			      msg = "<font color='red'>Email格式不正确!</font>";
			   span.innerHTML = msg;
			   return msg == "&nbsp;&nbsp;&nbsp;<img src='img/true.png' style='width:12px'>";
		};
		function resetForm() {
			var span1 = document.getElementById("commentContentSpan");
			var span2 = document.getElementById("emailSpan");
			var span3 = document.getElementById("usernameSpan");
			span1.style.display = "none";
			span1.innerHTML = "";
			span2.style.display = "none";
			span2.innerHTML = "";
			span3.style.display = "none";
			span3.innerHTML = "";
			$("#msgform input[type=text]").val('');
			$("#msgform input[type=email]").val('');
			$("#msgform textarea").val(''); 
			document.getElementById("msgform").reset(); 
		}
		function checkContent() {
			 var span = document.getElementById("commentContentSpan");
			 if(span.style.display != "block"){
					return;
				}
			 var passwordValue = contentObj.value;
			   var passwordRegex = /^\w{2,400}$/;
			   //$('#commentContentSpan').addClass("glyphicon glyphicon-ok");
			   var msg ="&nbsp;&nbsp;&nbsp;<img src='img/true.png' style='width:12px'>"; 

			   if (!passwordValue)
			    msg = "<font color='red'>内容不能为空</font>";
				
//			   else
//			    if (!passwordRegex.test(passwordValue)) {
//			     msg = "<font color='red'>评论内容不能大于400字</font>";
//			    }
			   span.innerHTML = msg;
			   return msg == "&nbsp;&nbsp;&nbsp;<img src='img/true.png' style='width:12px'>";
			};
			
			$('#msg_btn').click(function(){
				
				var bUsername = checkUserName();
				   var bPassword = checkContent();
				   var bEmail = checkEmail();
				if( bUsername && bPassword  && bEmail){
					$.ajax({
						type: "POST",
						url: "addMsg.action",
						data: $('#msgform').serialize(),
						dataType: 'json',
						success: function(result) {
							if(result.data != null){
								rerderComment(result.data);
								commentCnt = commentCnt + 1; 
								$("#comment_count").html(commentCnt+"次评论");
							}
							resetForm();
							 
						}
					});
				}else{
					
				}
			});
			function commentValidate() {
				 var articleIdObj = document.getElementById("blogArticleId");
				 articleIdObj.value = id;
				 
				 usernameObj.onfocus=function(){
					 var span = document.getElementById("usernameSpan");
					 span.style.display = "block";
				 };
				 usernameObj.onblur = function() {
					 checkUserName();
				 };
				
				contentObj.onfocus=function() {
					 var span = document.getElementById("commentContentSpan");
					 span.style.display = "block";
				};
				contentObj.onblur = function() {
					checkContent();
				};
				
				emailObj.onfocus=function() {
					var span = document.getElementById("emailSpan");
					span.style.display = "block";};
					
				 emailObj.onblur = function() {
					 checkEmail();
				};
				
				//var form = document.getElementById("msg_form");
				
				
		};
});


